﻿using System;
using System.Collections.Generic;
using Furion.DependencyInjection;
using Furion.LinqBuilder;
using Hu.Smart.Core;
using Hu.Smart.Core.Storage;
using Microsoft.Extensions.Caching.Memory;
using OfficeOpenXml;
using OfficeOpenXml.Style;

namespace Hu.Smart.Core.Excel
{
    public abstract class ExcelExporterBase
    {
        readonly ITempFileCacheManager _tempFileCacheManager;
        protected ExcelExporterBase(ITempFileCacheManager tempFileCacheManager)
        {
            _tempFileCacheManager = tempFileCacheManager;
        }

        protected FileDto CreateExcelPackage(string fileName, Action<ExcelPackage> creator)
        {
            var file = new FileDto(fileName, MimeTypeNames.ApplicationVndOpenxmlformatsOfficedocumentSpreadsheetmlSheet);
            ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
            using (var excelPackage = new ExcelPackage())
            {

                creator(excelPackage);
                //是否保存数据
                Save(excelPackage, file);
            }

            return file;
        }

        protected void AddHeader(ExcelWorksheet sheet, params string[] headerTexts)
        {
            if (headerTexts.IsNullOrEmpty())
            {
                return;
            }

            for (var i = 0; i < headerTexts.Length; i++)
            {
                AddHeader(sheet, i + 1, headerTexts[i]);
            }
        }

        protected void AddHeader(ExcelWorksheet sheet, int fromRow, int fromCol, params string[] headerTexts)
        {
            if (headerTexts.IsNullOrEmpty())
            {
                return;
            }

            for (var i = 0; i < headerTexts.Length; i++)
            {
                AddHeader(sheet, i + fromCol, headerTexts[i], fromRow);
            }
        }

        protected void AddHeader(ExcelWorksheet sheet, int col, string headerText, int row = 1)
        {
            var cell = sheet.Cells[row, col];
            cell.Value = headerText;
            cell.Style.Font.Bold = true;
            cell.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
            cell.Style.HorizontalAlignment = ExcelHorizontalAlignment.Left;
        }

        protected void AddObjects<T>(ExcelWorksheet sheet, int startRow, IList<T> items, params Func<T, object>[] propertySelectors)
        {
            if (items.IsNullOrEmpty() || propertySelectors.IsNullOrEmpty())
            {
                return;
            }

            for (var i = 0; i < items.Count; i++)
            {
                for (var j = 0; j < propertySelectors.Length; j++)
                {
                    sheet.Cells[i + startRow, j + 1].Value = propertySelectors[j](items[i]);
                }
            }
        }

        protected List<NameValue<int>> GetAllColumns(ExcelWorksheet sheet, int firstRow = 1)
        {
            List<NameValue<int>> columns = new List<NameValue<int>>();
            for (int i = 1; i <= sheet.Dimension.End.Column; i++)
            {
                columns.Add(new NameValue<int>(sheet.Cells[firstRow, i].Value.ToString(), i));
            }
            return columns;
        }

        protected void Save(ExcelPackage excelPackage, FileDto file)
        {
            _tempFileCacheManager.SetFile(file.FileToken, excelPackage.GetAsByteArray());
        }

    }
}